package com.yc.order.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yc.entity.Goodsinfo;
import com.yc.entity.OrderInfo;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public interface OrderInfoDao extends BaseMapper<OrderInfo> {

    /**
     *根据用户mno查询所属订单
     * @param mno
     * @return
     */
    @Select("SELECT orderinfo.* FROM memberinfo " +
            "JOIN addrinfo ON memberinfo.mno = addrinfo.mno " +
            "JOIN orderinfo ON addrinfo.ano = orderinfo.ano " +
            "WHERE memberinfo.mno = #{mno}  ORDER BY odate DESC")
    List<OrderInfo> findBymno(String mno);

    /**
     *根据用户编号查询所属订单详情个数  不是订单个数哦，是订单内部的个数和
     * @param mno
     * @return
     */
    @Select("SELECT m.mno, COUNT(oi.ono) AS total_orderitem FROM memberinfo m " +
            "JOIN addrinfo a ON m.mno = a.mno " +
            "JOIN orderinfo o ON o.ano = a.ano " +
            "JOIN orderiteminfo oi ON o.ono = oi.ono " +
            "WHERE m.mno = #{mno}")
    Map<String, Object> findItemCountByMno(String mno);


    @Select("select g.gno, num, price from goodsinfo g " +
            "inner join cartinfo c on g.gno = c.gno " +
            "where cno in (${cnos})")
    List<Map<String,Object>> selectGoodsByCnos(@Param("cnos") String cnos);

    @Insert("insert into orderinfo(ono,odate,status,price,invoice) values(#{ono},now(),1,#{totalPrice},0)")
    int insertOrder(String ono , Double totalPrice);

    @Insert("insert into orderiteminfo values(null,#{ono},#{gno},#{num},#{price},1)")
    int insertOrderItem(String ono ,Integer gno, Integer num,  Float price);

    @Update("update goodsinfo set balance = balance -#{num} where gno =#{gno}")
    int updateGoods(Integer num , Integer gno);

    @Delete("delete from cartinfo where cno in (${cnos})")
    int delCart(@Param("cnos") String cnos);

}
